﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Util.Database;

namespace ConfigureNZBLoader
{
    public partial class EditFeeds : Form
    {
        public EditFeeds()
        {
            InitializeComponent();
        }

        class FeedListAdapter
        {
            public string name;
            public uint id;

            public FeedListAdapter(uint id, string name)
            {
                this.name = name;
                this.id = id;
            }

            public override string ToString()
            {
                return name;
            }
        }



        private void EditFeeds_Load(object sender, EventArgs e)
        {
            using (IDbCommand cmd = DB.CreateCommand())
            {
                cmd.CommandText = "select * from feed";

                using (IDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        lstFeeds.Items.Add(new FeedListAdapter((uint)(long)dr["ID"], dr["NAME"].ToString()));
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddFeed dlg = new AddFeed();
            if (DialogResult.OK == dlg.ShowDialog(this))
            {
                using (IDbCommand cmd = DB.CreateCommand())
                {
                    cmd.CommandText = "insert into feed(name, address) values (?, ?)";
                    IDataParameter par = cmd.CreateParameter();
                    par.Value = dlg.FeedName;
                    cmd.Parameters.Add(par);
                    par = cmd.CreateParameter();
                    par.Value = dlg.Address;
                    cmd.Parameters.Add(par);
                    cmd.ExecuteNonQuery();

                    cmd.Parameters.Clear();
                    cmd.CommandText = "select * from feed where id = (select max(id) from feed)";
                    using (IDataReader dr = cmd.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            lstFeeds.Items.Add(new FeedListAdapter((uint)(long)dr["ID"], dr["NAME"].ToString()));
                        }
                    }
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            object obj = lstFeeds.SelectedItem;
            if (obj != null)
            {
                lstFeeds.Items.Remove(obj);

                using (IDbCommand cmd = DB.CreateCommand())
                {
                    cmd.CommandText = "delete from feed where id=?";
                    IDataParameter par = cmd.CreateParameter();
                    par.Value = ((FeedListAdapter)obj).id;
                    cmd.Parameters.Add(par);
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}
